/*
* 注：所有的混合的属性和方法都以mixin结尾
* */
const globolMixins = {
  data() {
    return {
      chooseFileNameMixin: '',
      /*多环境配置及token信息*/
      commonValueMixin: '',
      modalShowTitleMixin: '',
      VUE_APP_IMAGE_URL_PRE: '', // 图片前缀地址
      VUE_APP_BASE_URL: '', // 请求的url地址
      VUE_APP_BASE_WS_URL: '', // 请求的url地址
      accessTokenMixin: '', // 请求头的token
      userBaseInfoMixin: {}, // 用户信息
      startEndArrMixin: [],
      startEndArrSubMixin: [],
      /*时间点相关*/
      todayTimeMixin: '',
      currentTimeMixin: '',
      todayTimeMixinLast: '',
      yesterdayTimeMixin: null,
      beforeThreeDateTimeMixin: '',
      /*router跳转相关*/
      queryParamsMixin: null,
      subFormMixin:{},
      searchFormMixin:{}
    }
  },
  computed: {
    // ...mapState('user', ['token', 'userInfo'])
  },
  watch: {},
  created() {
    /* 获取url连接域名，用于多平台迁移*/
    const localUrl = window.location.href.slice(0, window.location.href.indexOf('/', 9) + 1)
    const socketUrl = localUrl.replace(/http|https/gi, 'ws')
    // 读取.env 多坏境里的数据
    this.VUE_APP_IMAGE_URL_PRE = process.env.VUE_APP_IMAGE_URL_PRE
    this.VUE_APP_BASE_URL = process.env.VUE_APP_BASE_URL
    this.VUE_APP_BASE_WS_URL = process.env.NODE_ENV === 'serve' ? process.env.VUE_APP_BASE_WS_URL : socketUrl
    /* 获取时间点*/
    this.todayTimeMixin = this.$momentMini().startOf('day').format('YYYY-MM-DD HH:mm:ss')
    this.currentTimeMixin = this.$momentMini().format('YYYY-MM-DD HH:mm:ss')
    this.todayTimeMixinLast = this.$momentMini().endOf('day').format('YYYY-MM-DD HH:mm:ss')
    this.beforeThreeDateTimeMixin = this.$momentMini().add(-3, 'days').format('YYYY-MM-DD HH:mm:ss')
    this.yesterdayTimeMixin = this.$momentMini().add(-1, 'days').format('YYYY-MM-DD HH:mm:ss')
    // 通用获取页面参数
    if (this.$route && this.$route.query && this.$route.query.params) {
      this.queryParamsMixin = JSON.parse(this.$route.query.params)
    }

    //let tableListData=res.data&&res.data.list
    //res.data.list && data
    //let data=[1,2,3,4]
    //let tableListData=res.data.list || data
  },
  mounted() {

  },
  methods: {
    // vue router跳转
    /*
    * router入栈跳转
    * path：跳转的路径名（name）
    * params: 跳转传递的参数（json格式）
    * */
    routerPushMixin(path, params) {
      let data = {}
      if (params) {
        data = {
          params: JSON.stringify(params)
        }
      } else {
        data = {}
      }
      this.$router.push({
        name: path,
        query: data
      })
    },
    routerReplaceMixin(path, params) {
      let data = {}
      if (params) {
        data = {
          params: JSON.stringify(params)
        }
      } else {
        data = {}
      }
      this.$router.replace({
        name: path,
        query: data
      })
    },
    routerBackMixin() {
      this.$router.go(-1)
    },
    /*
    * 清空空的参数项
    * objParam：传入的{}参数
    * */
    clearParamsIsNullMixin(objParam) {
      const obj = Object.keys(objParam)
      obj.forEach(fItem => {
        if (objParam[fItem] === '' || objParam[fItem] === null || objParam[fItem] === undefined) delete objParam[fItem]
      })
      return objParam
    },
    /*
    * 通知弹框
    * message：通知的内容
    * type：通知类型
    * duration：通知显示时长（ms）
    * */
    vantToastMixin(message, type, duration) {
      type = type || 'success'
      this.$toast({
        type: type,
        message: message,
        duration: "2000",
        forbidClick: true,
        overlay: false,
      });
    },
    /*
    * loading加载框
    * 调用后通过 this.loadingIdMixin.close() 进行关闭
    * */
    vantToastNoneMixin(msg){
      this.$toast(msg)
    },
    vantLoadingMixin() {
      this.loadingIdMixin = this.$toast.loading({
        message: '加载中',
        forbidClick: true,
        loadingType: 'spinner'
      });
    },
    /*
      确认弹框(没有取消按钮)
    * title:提示的标题
    * message:提示的内容
    * return Promise
    * */
    vantConfirmNoCancelBtnMixin(title, message) {
      return new Promise((resolve, reject) => {
        this.$dialog.confirm({
          title: title || '确认框',
          message: message || '你确定要删除吗',
        }).then(() => {
          resolve()
        }).catch(() => {
          reject()
        });
      })
    },
    /*
    * 确认弹框
    * title:提示的标题
    * message:提示的内容
    * return Promise
    * */
    vantAlterMixin(title, message) {
      return new Promise((resolve, reject) => {
        this.$dialog.alert({
          title:  title || '确认框',
          message: message || '确认弹框',
          theme: 'round-button',
        }).then(() => {
          resolve()
        }).catch(() => {
          reject()
        })
      })
    },
    /*
    * time:传入的时间ms
    * return Promise
    * */
    sleepMixin(time) {
      return new Promise((resolve, reject) => {
        const timer = setTimeout(() => {
          clearTimeout(timer)
          resolve()
        }, time)
      })
    },
  }
}

export default globolMixins
